/*删除确认*/
function del_check(url) {
    var re = window.confirm('确认删除此内容?');
    if (re) {
        location.href = url;
    }
}

/*留言 审核通过 确认*/
function confirm_check(url) {
    var re = window.confirm('确认审核通过此留言信息?');
    if (re) {
        location.href = url;
    }
}


/*新建店铺*/
function create_mall() {
    var mallname = $('#mallname').val();
    var username = $('#username').val();
    var password = $('#password').val();
    /*表单验证*/
    var id_list = ['mallname', 'username', 'password'];
    if (!check_require(id_list)) {
        alert('请填写完整信息');
        return;
    }
    $.ajax({
        url: '/index.php/back/mall-new/',
        type: 'post',
        data: {
            mallname: mallname,
            username: username,
            password: password,
        },
        success: function (re) {
            var re_json = JSON.parse(re);
            if (re_json['status'] == 'error') {
                alert('店铺创建失败,请重新输入');
                location.reload();
            }
            else {
                location.href = re_json['url'];
            }

        },

    });
}

/*新建桌子*/
function create_desk() {
    var deskname = $('#deskname').val();
    var mall_id = $('#mall_id').val();
    /*表单验证*/
    var id_list = ['deskname', 'mall_id'];
    if (!check_require(id_list)) {
        alert('请填写完整信息');
        return;
    }
    $.ajax({
        url: '/index.php/back/desk-new/',
        type: 'post',
        data: {
            deskname: deskname,
            mall_id: mall_id,
        },
        success: function (re) {
            var re_json = JSON.parse(re);
            if (re_json['status'] == 'error') {
                alert('桌子创建失败,请重新输入');
                location.reload();
            }
            else {
                location.href = re_json['url'];
            }

        },

    });
}

/*非空验证工具*/
function check_require(id_list) {
    for (i = 0; i < id_list.length; i++) {
        console.log(id_list[i]);
        var temp = $('#' + id_list[i]).val();
        if (temp == undefined || temp == '') {
            console.log('no');
            console.log(temp);
            return false;
        }
    }
    return true;
}

/*文件替换label提示更改*/
function file_chage(th) {
    var v = $(th).val();
    $(th).parent().children('.custom-file-label').text(v);
}

/*密码修改*/
function pass_change() {
    var old_pass = $('#old_pass').val();
    var new_pass = $('#new_pass').val();
    var pass_confirm = $('#pass_confirm').val();
    console.log(old_pass == '');
    console.log(new_pass == '');
    console.log(pass_confirm == '');
    if (old_pass == '' || new_pass == '' || pass_confirm == '') {
        alert('请填写完整信息');
        return;
    }
    if (new_pass != pass_confirm) {
        alert('两次输入密码不同请重新输入');
        return;
    }
    $.ajax({
        url: '/back/pass-change',
        type: 'post',
        data: {
            old_pass: old_pass,
            new_pass: new_pass,
        },
        success: function (res) {
            var json = JSON.parse(res);
            if (json.status == 'error') {
                alert('更改失败请重新确认输入');
                return;
            }
            else {
                location.href = json.url;
            }
            console.log(json.status);
        }
    });
}